package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Map;

import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.huawei.ProductVo;
import com.zzyl.common.utils.huawei.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 产品模块Controller
 *
 * @author ruoyi
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "产品模块相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询产品模块列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取产品模块列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 1.1 从物联网平台同步产品列表
     *
     * @return
     */
    @PostMapping("syncProductList")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 1.2 查询所有产品列表
     *
     * @return
     */
    @GetMapping("allProduct")
    public R<List<ProductVo>> allProduct() {
        List<ProductVo> productVos = deviceService.allProduct();
        return R.ok(productVos);
    }

    /**
     * 1.3 注册设备
     *
     * @param deviceDto
     * @return
     */
    @PostMapping("register")
    public AjaxResult register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 1.5 查询设备详细数据
     *
     * @param iotId
     * @return
     */
    @GetMapping("{iotId}")
    public R<DeviceDetailVo> queryDeviceDail(@PathVariable String iotId) {
        DeviceDetailVo devices = deviceService.queryDeviceDail(iotId);
        return R.ok(devices);
    }

    /**
     * 1.6 查看设备上报的数据
     *
     * @param iotId
     * @return
     */
    @GetMapping("queryServiceProperties/{iotId}")
    public R<List> queryServiceProperties(@PathVariable String iotId) {
        List<Map> maps = deviceService.queryServiceProperties(iotId);
        return R.ok(maps);
    }

    /**
     * 1.7删除设备
     *
     * @param iotId
     * @return
     */
    @DeleteMapping("{iotId}")
    public AjaxResult delete(@PathVariable String iotId) {
        deviceService.delete(iotId);
        return success();
    }

    /**
     * 1.8修改设备
     *
     * @param device
     * @return
     */
    @PutMapping
    public AjaxResult updateV(@RequestBody Device device) {
        deviceService.updateV(device);
        return success();
    }
}
